﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;

namespace StonePonyMVC.StonePony.Classes
{
    [Serializable]
    public class ImageDescriptor
    {
        private static string _rootDir = null;
        private string RootDir
        {
            get
            {
                if (String.IsNullOrEmpty(_rootDir))
                    _rootDir = ConfigurationManager.AppSettings["ImageRootDirectory"];
                return _rootDir;
            }
        }

        /// <summary>
        /// <code>items.item_code</code>
        /// </summary>
        public int ItemCode { get; set; }

        /// <summary>
        /// <code>items.item_shipment</code>
        /// </summary>
        public int ItemShipment { get; set; }

        /// <summary>
        /// <code>items.item_stock_code</code>
        /// </summary>
        public string ItemStockCode { get; set; }

        public string ItemShortDescription
        {
            get { return String.Format("{0} {1}", ItemShipment, ItemStockCode); }
        }

        /// <summary>
        /// <code>items.item_title</code>
        /// </summary>
        public string ItemTitle { get; set; }

        /// <summary>
        /// <code>items.item_notes</code>
        /// </summary>
        public string ItemNotes { get; set; }

        /// <summary>
        /// <code>items.item_image_small</code>
        /// </summary>
        public string ItemImageSmall { get; set; }
        public string SmallImageUrl { get; private set; }
        public string SmallImageUrlFull { get; private set; }   // For letter generation

        /// <summary>
        /// <code>items.item_image_large</code>
        /// </summary>
        public string ItemImageLarge { get; set; }
        public string LargeImageUrl { get; private set; }

        /// <summary>
        /// <code>items.status</code>
        /// </summary>
        public string Status { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Price { get; set; }

        /// <summary>
        /// Builds absolute URL for small image
        /// </summary>
        /// <param name="url">URL of the Home page</param>
        /// <param name="imageDir">Relative path to small images</param>
        public void SetSmallImage(string imageDir, HttpServerUtilityBase server, string noImage, string pathModifier,
            string rootUrl = null)
        {
            if (!String.IsNullOrEmpty(ItemImageSmall) && ImageExists(server, imageDir, ItemImageSmall))
                SmallImageUrl = String.IsNullOrEmpty(pathModifier) 
                    ? Path.Combine(imageDir, ItemShipment.ToString(), ItemImageSmall).Replace(@"\", "/")
                    : Path.Combine(imageDir, pathModifier, ItemShipment.ToString(), ItemImageSmall).Replace(@"\", "/");
            else
                SmallImageUrl = Path.Combine(imageDir, noImage).Replace(@"\", "/");
            if (!String.IsNullOrEmpty(rootUrl))
                this.SmallImageUrlFull = Path.Combine(rootUrl, this.SmallImageUrl);
        }

        /// <summary>
        /// Builds absolute URL for large image
        /// </summary>
        /// <param name="url">URL of the Home page</param>
        /// <param name="imageDir">Relative path to large images</param>
        public void SetLargeImage(string imageDir, HttpServerUtilityBase server, string noImage)
        {
            if (!String.IsNullOrEmpty(ItemImageLarge) && ImageExists(server, imageDir, ItemImageLarge))
                LargeImageUrl = Path.Combine(imageDir, ItemShipment.ToString(), ItemImageLarge).Replace(@"\", "/");
            else
                LargeImageUrl = Path.Combine(imageDir, noImage).Replace(@"\", "/");
        }
        
        /// <summary>
        /// Verifies image URL
        /// </summary>
        /// <param name="server"><see cref="HttpServerUtilityBase"/></param>
        /// <param name="imageDir">'shipments' or 'images'</param>
        /// <param name="imageFile">File name</param>
        /// <returns><code>true</code> - file exists</returns>
        private bool ImageExists(HttpServerUtilityBase server, string imageDir, string imageFile)
        {
            string virtualPath = String.Format("{0}/{1}/{2}/{3}", this.RootDir, imageDir, this.ItemShipment, imageFile);
            string physicalPath = server.MapPath(virtualPath);
            return File.Exists(physicalPath);
        }
    }
}